#include <iostream>
#include <map>
#include <string>
#include <utility>

using std::cout;
using std::endl;
using std::map;
using std::string;
using std::pair;

void test()
{
    //map的特征
    //1、存放的是pair类型，也就是key-value类型，key值是唯一的
    //不能重复，value值可以重复，也可以不重复
    //2、默认情况下，会按照key值进行升序排列
    //3、底层使用的也是红黑树
    //初始化
    map<int, string> number = {
        pair<int, string>(1,"beijing"),
        pair<int, string>(4,"wangdao"),
        pair<int, string>(3,"beijing"),
        {5, "nanjing"},
        {1, "nanjing"},
        {2, "dongjing"},
    };

    //遍历
    for(auto it = number.begin(); it != number.end(); ++it)
    {
        cout << it->first << "  " << it->second << endl;
    }

    cout << endl << "map的查找" << endl;
    size_t cnt  = number.count(3);
    size_t cnt2  = number.count(10);
    cout << "cnt = " << cnt << endl;
    cout << "cnt2 = " << cnt2 << endl;

    cout << endl;
    auto it2 = number.find(3);
    if(it2 != number.end())
    {
        cout << "该元素存在map中 " 
             << it2->first 
             << "  "
             << it2->second << endl;
    }
    else
    {
        cout << "该元素不存在map中" << endl;
    }

    cout << endl << "set的插入操作" << endl;
    auto ret = number.insert({6, "wuhan"});//插入的类型是pair类型
    if(ret.second)
    {
        cout << "插入成功 " 
             << ret.first->first << "  "
             << ret.first->second << endl;
    }
    else
    {
        cout << "插入失败了,元素已经存在set中"<< endl;
    }
    for(auto it = number.begin(); it != number.end(); ++it)
    {
        cout << it->first << "  " << it->second << endl;
    }
    cout << endl;

    cout << endl << "map的下标" << endl;
    cout << "number[1] = " << number[1] << endl;//查找
    cout << "number[7] = " << number[7] << endl;//插入

    cout << endl << endl;
    for(auto it = number.begin(); it != number.end(); ++it)
    {
        cout << it->first << "  " << it->second << endl;
    }
    cout << endl;

    cout << endl << endl;
    number[7] = "霸气外露";//修改
    number[3] = "霸气外露3";
    for(auto it = number.begin(); it != number.end(); ++it)
    {
        cout << it->first << "  " << it->second << endl;
    }
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

